package com.zhong.gang.app.ui.waidiao.input.controller;

import android.app.Activity;
import android.text.TextUtils;
import com.luck.picture.lib.tools.ToastUtils;
import com.zhong.gang.app.api.Api;
import com.zhong.gang.app.api.HttpUtils;
import com.zhong.gang.app.mvp.CommonPresenter;
import com.zhong.gang.app.ui.main.MainActivity;
import com.zhong.gang.applib.http.BaseDataBean;
import com.zhong.gang.applib.http.callback.DataCallBack;
import com.zhong.gang.applib.rx.RxManager;
import com.zhong.gang.applib.ui.matching.MatchingBean;
import java.io.File;

public class WaiDiaoDataPresenter {
    private Activity activity;

    public WaiDiaoDataPresenter(Activity activity) {
        this.activity = activity;

    }


    public void upData(File clFile,File hwFile,int endLocalId,MatchingBean matchingBean,int payType
            ,double startLocalGrossWeight,double startLocalTareWeight,double startLocalSlagWeight,int carType,String consignorCarNum,int dispatchingCarId,String materialTypeList
            , double floatAmount){
        if (clFile == null){
            ToastUtils.s(activity,"请选择车辆照片");
            return;
        }
        if (hwFile == null){
            ToastUtils.s(activity,"请选择货物照片");
            return;
        }

        if (carType == 0){
            if (dispatchingCarId <=0){
                ToastUtils.s(activity,"请选择派车单");
                return;
            }
        }else {
            if (endLocalId<=0){
                ToastUtils.s(activity,"请选择目的地");
                return;
            }
            if (TextUtils.isEmpty(consignorCarNum)){
                ToastUtils.s(activity,"请输入车牌号");
                return;
            }
        }


        if (matchingBean == null){
            ToastUtils.s(activity,"请选择货主");
            return;
        }
        if (TextUtils.isEmpty(materialTypeList)){
            ToastUtils.s(activity,"请填写完整料型");
            return;
        }
        CommonPresenter.upImg(activity,clFile,new DataCallBack<BaseDataBean<String>>(activity){
            @Override
            public void onResponse(BaseDataBean<String> data1) throws Exception {
                CommonPresenter.upImg(activity,hwFile,new DataCallBack<BaseDataBean<String>>(activity){
                    @Override
                    public void onResponse(BaseDataBean<String> data2) throws Exception {
                        materialReceive(data1.getResult(),data2.getResult(),endLocalId,matchingBean,payType
                                ,startLocalGrossWeight,startLocalTareWeight,startLocalSlagWeight,carType,consignorCarNum,dispatchingCarId,materialTypeList,floatAmount);
                    }
                });
            }
        });
    }
    /**
     * 上传信息
     */
    private void materialReceive(String carPhoto,String materialTypePhoto,int endLocalId,MatchingBean matchingBean,int payType
            ,double startLocalGrossWeight,double startLocalTareWeight,double startLocalSlagWeight,int carType,String consignorCarNum,int dispatchingCarId,String materialTypeList
            , double floatAmount){
        HttpUtils.post(Api.materialAdd,activity)
                .addParams("endLocalId",endLocalId+"")//货主自有车辆送货地点
                .addParams("consignorId",matchingBean.getConsignor().getId()+"")//货主ID
                .addParams("consignorBankCardId",(matchingBean.getConsignorBankCard() != null)? (matchingBean.getConsignorBankCard().getId()+""):null)//货主银行卡ID
                .addParams("payType",payType+"")//0打款放车,1钢厂结算
                .addParams("startLocalGrossWeight",startLocalGrossWeight+"")//装货现场毛重
                .addParams("startLocalTareWeight",startLocalTareWeight+"")//装货现场皮重
                .addParams("startLocalSlagWeight",startLocalSlagWeight+"")//装货现场扣渣
                .addParams("carType",carType+"")//0公司派车,1货主自有车辆
                .addParams("consignorCarNum",consignorCarNum+"")//货主自有车辆车牌号
                .addParams("carPhoto",carPhoto)//车辆照片
                .addParams("materialTypePhoto",materialTypePhoto)//货物照片
                .addParams("dispatchingCarId",dispatchingCarId<=0?null:(dispatchingCarId+""))//派车单ID
                .addParams("materialTypeList",materialTypeList)//料型列表,json数组,[{materialTypeId:0, price:0, weight:0}]
                .addParams("floatType",(floatAmount<=0?2:1)+"")//涨扣类型:1涨，2扣
                .addParams("floatAmount",Math.abs(floatAmount)+"")//涨扣金额
                .build().execute(activity,"上传信息",new DataCallBack<BaseDataBean<String>>(activity){
            @Override
            public void onResponse(BaseDataBean<String> data) throws Exception {
                super.onResponse(data);
                activity.finish();
                new RxManager().post(MainActivity.MAIN_ADD_OK);
            }
        });
    }


}